package com.nginxmanager.controller;

import com.nginxmanager.NginxManagerApplication;
import com.nginxmanager.domain.Conf;
import com.nginxmanager.domain.Model;
import com.nginxmanager.domain.System;
import com.nginxmanager.server.ConfRepository;
import com.nginxmanager.server.SystemRepository;
import com.nginxmanager.server.config.nginxConf.AllNginxConf;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-01-18.
 */

@RestController
@RequestMapping("/nginx")
@Api(tags = "Nginx系统信息获取Api")
public class SystemController {
    private final Logger log = LoggerFactory.getLogger(SystemController.class);
    @Autowired
    SystemRepository systemRepository;
    @Autowired
    ConfRepository confRepository;
    @GetMapping("/getSystem")
    @ApiOperation("查看系统配置")
    public ResponseEntity<List<System>> getbypage (){
        log.debug("查看系统测试开始了");
        List<System> systems = new ArrayList<>();
        systems = systemRepository.findAll();
        return ResponseEntity.status(200).body(systems);
    }

    @PostMapping("/updateSystem")
    @ApiOperation(value="修改系统配置")
    @ApiResponses(value={
            @ApiResponse(code = 202, message = "修改系统配置成功"),
            @ApiResponse(code = 200, message = " 创建系统信息成功"),
            @ApiResponse(code = 401, message = " 配置不存在"),
    })
    public ResponseEntity<System> updateSystemInfo(@RequestBody System system){
        log.error("系统测试开始了");
        if (system.getId()!=null){
            System system1 = systemRepository.findOne(system.getId());
            if (system1==null){
                return ResponseEntity.status(401).body(null);
            }
            log.error("系统数据"+system.getName());
            if(system.getName().equals("listen")){
                log.error("进入判断了吗");
                Conf conf = confRepository.findByKey("listen");
                System system2= systemRepository.findByName("filePath");
                log.error("系统测试开始了"+system2.toString());
                conf.setValue(system.getValue());
                log.error("系统测试开始了"+conf.toString());
                AllNginxConf allNginxConf = new AllNginxConf();
                List<String> serverList = new ArrayList<>();
		        serverList =allNginxConf.upt(system2.getValue(),conf);
                confRepository.save(conf);
                log.debug("系统测试开始了"+serverList.size());
            }
            systemRepository.save(system);
            return ResponseEntity.status(202).body(system);
        }
        if(system.getName().equals("listen")){
            System system3= systemRepository.findByName("filePath");
            Conf conf = confRepository.findByKey("listen");
            conf.setValue(system.getValue());
            AllNginxConf allNginxConf = new AllNginxConf();
            List<String> serverList = new ArrayList<>();
            serverList =allNginxConf.upt(system3.getValue(),conf);
            confRepository.save(conf);
            log.debug("系统测试开始了"+serverList.size());
        }
        systemRepository.save(system);
        return ResponseEntity.status(200).body(system);
    }

    @DeleteMapping("/deleteSystem")
    @ApiOperation(value="删除系统配置")
    @ApiResponses(value={
            @ApiResponse(code = 401, message = " 配置不存在"),
            @ApiResponse(code = 402, message = " 参数有误"),
    })
    public ResponseEntity<System> delSystemInfo(@RequestBody System system){
        if (system.getId()!=null){
            System system1 = systemRepository.getOne(system.getId());
            if (system1==null){
                return ResponseEntity.status(401).body(null);
            }
            systemRepository.delete(system);
            return ResponseEntity.status(200).body(system);
        }
        return  ResponseEntity.status(402).body(system);
    }
}
